package yuan.userservice9999.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import yuan.userservice9999.feign_client.ProductClient;

import java.util.List;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/17 17:08
 */


@SuppressWarnings("ALL")
@Slf4j
@RestController
@RequestMapping("/user")
public class MyController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    DiscoveryClient discoveryClient;


    @Autowired
    LoadBalancerClient loadBalancedClient;

    @GetMapping("/getProduct")
    public String getProduct(){
        log.info("调用product之前");

        String res = restTemplate.getForObject("http://product-service/product/showProduct", String.class);
        log.info("调用product之后");
        log.info(res);
        return res;


    }


    @GetMapping("/discoveryClinetList")
    public List<ServiceInstance> list(){
        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
        log.info(String.valueOf(instances.size()));

        return instances;
    }

  /*  @GetMapping("/loadbalance")
    public ServiceInstance li1st() {

        return loadBalancedClient.choose("uproduct-service-9998");
    }*/



    @GetMapping("/loadbalanceRestTemplate")
    public String li1st() {

        return restTemplate.getForObject("http://product-service/product/showProduct", String.class);
    }

    @Autowired
    ProductClient productClient;

    @GetMapping("/productFeignClient")
    public String productFeignClient() {

        return productClient.getProduct();
    }

    @GetMapping("/productFeignClientParam")
    public String productFeignClientParam() {

        return productClient.getProduct(String.valueOf(12));
    }


    @GetMapping("/hystrixtest1")
    public String hystrixtest1(@RequestParam int id){
        return productClient.hystrixtest1(id);
    }


    @GetMapping("/hystrixtest2")
    public String hystrixtest2(@RequestParam int id){
        return productClient.hystrixtest2(id);
    }




}
